﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Models;

namespace Workflow
{
    public partial class Add : System.Web.UI.Page
    {
        private WorkflowManager objWorkflowManager = new WorkflowManager();
        private UserTeamRoleManager objUserTeamRoleManager = new UserTeamRoleManager();
        int workMouldId;
        public static string workName;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                workMouldId = Convert.ToInt32(Request.QueryString["WorkMouldId"]);
                workName = String.IsNullOrEmpty(Request.QueryString["workName"]) ? "" : Request.QueryString["workName"].ToString();
                this.txtUserName.Text = Session["DisplayName"].ToString();
                List<UserTeamRole> workList = new List<UserTeamRole>();
                int count = objUserTeamRoleManager.GetUserInfo(Session["FullName"].ToString(), 2, workMouldId);
                if(count==0)
                {
                    string msg = string.Format("<script>alert('您没有权限创建此工作流');</script>");
                    Page.ClientScript.RegisterStartupScript(Page.GetType(), "", msg);
                }
                else
                {
                    Button1.Visible = true;
                }
                //workList = objWorkflowManager.GetUserCreateWorkflow(Session["FullName"].ToString());
                //this.ddlWorkflowName.DataSource = workList;
                //this.ddlWorkflowName.DataTextField = "WorkName";
                //this.ddlWorkflowName.DataValueField = "WorkMouldId";
                //this.ddlWorkflowName.SelectedIndex = 0;
                //this.ddlWorkflowName.DataBind();

                List<UserTeamRole> userList = new List<UserTeamRole>();
                userList = objUserTeamRoleManager.GetUserTeam(Session["FullName"].ToString());
                this.ddlTeam.DataSource = userList;
                this.ddlTeam.DataTextField = "TeamName";
                this.ddlTeam.DataValueField = "TeamId";
                this.ddlTeam.SelectedIndex = 0;
                this.ddlTeam.DataBind();

                string[] critical = new string[2]
                {
                    "普通",
                    "紧急"
                };
                this.ddlCritical.DataSource = critical;
                this.ddlCritical.SelectedIndex = 0;
                this.ddlCritical.DataBind();

            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            //if(this.ddlWorkflowName.Items.Count==0)
            //{
            //    string msg = string.Format("<script>alert('{0}');</script>", "没有可创建的工作流");
            //    Page.ClientScript.RegisterStartupScript(Page.GetType(), "alert", msg);
            //    return;
                
            //}
            workMouldId = Convert.ToInt32(Request.QueryString["WorkMouldId"]);
            if (this.ddlTeam.Items.Count == 0)
            {
                string msg = string.Format("<script>alert('{0}');</script>", "没有组织单元信息，请联系管理员");
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "alert", msg);
                return;

            }
            if (Session["FullName"].ToString() == null)
            {
                string msg = string.Format("<script>alert('{0}');</script>", "长时间没有操作，请刷新页面");
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "alert", msg);
                return;

            }
            WorkflowMould workMould=null;
            List<WorkflowChildMould> workChlidMould = new List<WorkflowChildMould>();
            objWorkflowManager.GetWorkflowMould(workMouldId, out workMould,out workChlidMould);
            try
            {
                
                objWorkflowManager.InsertWorkflow(workMould, workChlidMould, Session["FullName"].ToString(), Convert.ToInt32(this.ddlTeam.SelectedValue), this.ddlCritical.SelectedValue.ToString(),this.txtContent.Text.Trim().ToString());
                string msgForm = string.Format("<script>alert('创建成功！');window.location.href = '{0}';</script>", "Status.aspx?statusDefine=0");
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "", msgForm);
                //Response.Redirect("Status.aspx?statusDefine=0"); 

            }
            catch (Exception ex)
            {
                string msg = string.Format("<script>alert('{0}');</script>",ex.Message.ToString());
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "", msg);
            }

        }
    }
}